package com.bestv.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

/**
 * Created by charle-chen on 15/11/26.
 */
public class GlobalSettings {

    private Properties properties = new Properties();
    String configFile;

    private String oracleDbDriver;
    private String oracleDbUrl;
    private String oracleUsername;
    private String oralcePassword;

    private String mysqlDbDriver;
    private String mysqlDbUrl;
    private String mysqlUsername;
    private String mysqlPassword;

    private int threadNum;

    private static GlobalSettings instance;

    private GlobalSettings() {
        InputStream settingsInputStream = getClass().getResourceAsStream("/prop.properties");
        Properties settingsProperties = new Properties();

        try {
            settingsProperties.load(settingsInputStream);
            configFile = settingsProperties.getProperty("SETTINGS_FILE", "../config/settings.properties");
            parseConfig(configFile);
            setConfigFile(configFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static synchronized GlobalSettings getInstance() {
        if (instance == null) {
            instance = new GlobalSettings();
        }
        return instance;
    }

    private void parseConfig(String configFile){
        try {
            InputStream configInputStream = new FileInputStream(configFile);
            InputStreamReader configReader = new InputStreamReader(configInputStream, "UTF-8");
            properties.load(configReader);

            oracleDbDriver = properties.getProperty("oracle_jdbc_driver","oracle.jdbc.driver.OracleDriver");
            oracleDbUrl = properties.getProperty("oracle_db_url","jdbc:oracle:thin:@10.61.13.144:1521:orcl");
            oracleUsername = properties.getProperty("oracle_user","bestvis");
            oralcePassword = properties.getProperty("oracle_password","bestvdba");

            mysqlDbDriver = properties.getProperty("mysql_jdbc_driver","com.mysql.jdbc.Driver");
            mysqlDbUrl = properties.getProperty("mysql_db_url","jdbc:mysql://127.0.0.1:3306/vis_search_engine?useUnicode=true&amp;characterEncoding=UTF-8&allowMultiQueries=true");
            mysqlUsername = properties.getProperty("mysql_user","root");
            mysqlPassword = properties.getProperty("mysql_password","mysql");

            threadNum = Integer.parseInt(properties.getProperty("thread_num","5"));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void setConfigFile(String configFile) {
        this.configFile = configFile;
    }

    public String getConfigFile() {
        return configFile;
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public String getOracleDbDriver() {
        return oracleDbDriver;
    }

    public void setOracleDbDriver(String oracleDbDriver) {
        this.oracleDbDriver = oracleDbDriver;
    }

    public String getOracleDbUrl() {
        return oracleDbUrl;
    }

    public void setOracleDbUrl(String oracleDbUrl) {
        this.oracleDbUrl = oracleDbUrl;
    }

    public String getOracleUsername() {
        return oracleUsername;
    }

    public void setOracleUsername(String oracleUsername) {
        this.oracleUsername = oracleUsername;
    }

    public String getOralcePassword() {
        return oralcePassword;
    }

    public void setOralcePassword(String oralcePassword) {
        this.oralcePassword = oralcePassword;
    }

    public String getMysqlDbDriver() {
        return mysqlDbDriver;
    }

    public void setMysqlDbDriver(String mysqlDbDriver) {
        this.mysqlDbDriver = mysqlDbDriver;
    }

    public String getMysqlDbUrl() {
        return mysqlDbUrl;
    }

    public void setMysqlDbUrl(String mysqlDbUrl) {
        this.mysqlDbUrl = mysqlDbUrl;
    }

    public String getMysqlUsername() {
        return mysqlUsername;
    }

    public void setMysqlUsername(String mysqlUsername) {
        this.mysqlUsername = mysqlUsername;
    }

    public String getMysqlPassword() {
        return mysqlPassword;
    }

    public void setMysqlPassword(String mysqlPassword) {
        this.mysqlPassword = mysqlPassword;
    }


    public int getThreadNum() {
        return threadNum;
    }

    public void setThreadNum(int threadNum) {
        this.threadNum = threadNum;
    }
}
